package com.qf.service.impl;

import com.qf.bean.Zhanghu;
import com.qf.dao.ZhangHuDao;
import com.qf.service.ZhanghuService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 2019/11/15
 * Administrator
 * spring_anno_aop_1113
 * 面向对象面向君  不负代码不负卿
 */
public class ZhanghuServiceImpl implements ZhanghuService {
    private ZhangHuDao dao;

    public void setDao(ZhangHuDao dao) {
        this.dao = dao;
    }


    @Transactional
    public int zhuanzhang(int chuid, int ruid, int money) {
        //业务逻辑
        Zhanghu zhanghu = dao.findbyid(chuid);
        int k=0;
       if(zhanghu==null){
           System.out.println("转账人不存在");
       }else{
            if(money>zhanghu.getYue()){
                System.out.println("余额不足");
            }else{
                 //判断另外的账户是否存在
                Zhanghu zhanghu1 = dao.findbyid(ruid);
                if(zhanghu1==null){
                    System.out.println("收账人不存在");
                }else{
                    dao.geiqian(chuid,money);
                   // System.out.println(5/0);
                    dao.shouqian(ruid,money);
                    k=1;
                }
            }
       }
        return k;
    }


    @Transactional
    public int inserta() {
        int i = dao.inserta();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(5/0);
        return i;
    }

    //@Transactional(isolation = Isolation.READ_UNCOMMITTED)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public List<Zhanghu> finda() {
        return dao.finda();
    }


    @Transactional(isolation=Isolation.REPEATABLE_READ)
    public List findb() {
        //查询两次
        List before = dao.findb();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List after = dao.findb();

        ArrayList arrayList = new ArrayList();
        arrayList.add(before);
        arrayList.add(after);
        return arrayList;
    }

    @Transactional
    public int updateb() {
        return dao.updateb();
    }
}
